package com.aliwo.controller;

import com.aliwo.domain.Video;
import com.aliwo.service.VideoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:xuyayuan
 * @date:2021/7/18 20:10
 * @description: 普通用户权限
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("api/v1/videos")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @GetMapping("page")
    public ResponseEntity<Object> page(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "10") int size) {
        PageHelper.startPage(page, size);
        List<Video> list = videoService.findAll();
        PageInfo<Video> pageInfo = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>(5);
        map.put("total_size", pageInfo.getTotal()); // 总条数
        map.put("total_page", pageInfo.getPages()); // 总页数
        map.put("current_page", page); // 当前页
        map.put("data", pageInfo.getList()); // 数据
        return ResponseEntity.ok(map);
    }

    @GetMapping("findAll")
    public ResponseEntity<List<Video>> findAll() {
        return ResponseEntity.ok(videoService.findAll());
    }

    @GetMapping("findById")
    public ResponseEntity<Video> findById(@RequestParam("id") Integer id) {
        return ResponseEntity.ok(videoService.findById(id));
    }


}
